package com.example.yuejian23.yuejian_23.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian23.yuejian_23.Data.Mdata;
import com.example.yuejian23.yuejian_23.R;
import com.example.yuejian23.yuejian_23.javaBean.BisaiBean;
import com.example.yuejian23.yuejian_23.javaBean.DashiBean;
import com.example.yuejian23.yuejian_23.javaBean.GuanzhuBean;
import com.example.yuejian23.yuejian_23.javaBean.GuanzhusBean;
import com.example.yuejian23.yuejian_23.javaBean.HuodongBean;
import com.example.yuejian23.yuejian_23.service.GuanzhuService;
import com.example.yuejian23.yuejian_23.service.HomeService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DashiActivity extends AppCompatActivity {
    String dashi;
    TextView dashi1_name;
    ImageView dashi1_pic,xin;
    TextView name1;
    TextView jieshao1;
    int guanzhu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashi);

        intview();
        Bundle bundle = this.getIntent().getExtras();
        dashi = bundle.getString("dashiid");
        xin = (ImageView) findViewById(R.id.xin);
        xin.setOnClickListener(new guanzhus());

        getjulebudetil();
        guanzhu();

    }

    private void guanzhu() {
        int flag = 1;
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<GuanzhuBean> call = retrofit.create(GuanzhuService.class).guanzhu(Mdata.uid,dashi,flag);
        call.enqueue(new Callback<GuanzhuBean>() {
            @Override
            public void onResponse(Call<GuanzhuBean> call, Response<GuanzhuBean> response) {
                int state = response.body().getCode();
                guanzhu = state;
                if (state==1){
                    xin.setBackgroundResource(R.drawable.ic_xin);
                }else {
                    xin.setBackgroundResource(R.drawable.ic_xin1);
                }
            }
            @Override
            public void onFailure(Call<GuanzhuBean> call, Throwable t) {

            }
        });
    }

    private class guanzhus implements View.OnClickListener {
        int flag = 1;
        @Override
        public void onClick(View v) {
            Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            Call<GuanzhusBean> call = retrofit.create(GuanzhuService.class).guanzhus(Mdata.uid,dashi,flag);
            call.enqueue(new Callback<GuanzhusBean>() {
                @Override
                public void onResponse(Call<GuanzhusBean> call, Response<GuanzhusBean> response) {
                    int res = response.body().getCode();
                    if (res==1){
                        if (guanzhu==1){
                            Toast.makeText(DashiActivity.this,"关注成功",Toast.LENGTH_LONG).show();
                            guanzhu = 0;
                            xin.setBackgroundResource(R.drawable.ic_xin1);
                        }else {
                            Toast.makeText(DashiActivity.this,"取消关注",Toast.LENGTH_LONG).show();
                            guanzhu = 1;
                            xin.setBackgroundResource(R.drawable.ic_xin);
                        }
                    }
                }
                @Override
                public void onFailure(Call<GuanzhusBean> call, Throwable t) {

                }
            });

        }
    }

    public void getjulebudetil() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<DashiBean> call = retrofit.create(HomeService.class).getxiangss(dashi);
        call.enqueue(new Callback<DashiBean>() {
            @Override
            public void onResponse(Call<DashiBean> call, Response<DashiBean> response) {
                DashiBean myda = response.body();
                dashi1_name.setText(myda.getMname());
                //Toast.makeText(DashiActivity.this,"hfghjk",Toast.LENGTH_LONG).show();
                name1.setText(myda.getMname());
                jieshao1.setText(myda.getMintro());
                Picasso.with(DashiActivity.this).load("http://115.159.93.244/new18yuejian/Public/upload/"+myda.getMpic()).into(dashi1_pic);
            }

            @Override
            public void onFailure(Call<DashiBean> call, Throwable t) {

            }
        });

    }
    private void intview() {
        dashi1_name = (TextView) findViewById(R.id.dashi1_name);
        dashi1_pic = (ImageView) findViewById(R.id.dashi1_pic);
        name1 = (TextView) findViewById(R.id.name1);
        jieshao1 = (TextView) findViewById(R.id.jieshao1);
    }
}
